package com.zryc.shop.ums.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.ums.entity.Resource;
import com.zryc.shop.ums.service.IResourceService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 * 后台资源表 前端控制器
 * </p>
 *
 * @author 王志平
 * @since 2024-10-01
 */
@RestController
@RequestMapping("/ums/resource")
@Api(tags = "资源管理接口列表")
public class ResourceController {
    @Autowired
    private IResourceService resourceService;

    @PostMapping("/create")
    @ApiOperation("创建一个资源")
    public ResponseResult create(@RequestBody Resource resource) {
        resource.setCreateTime(LocalDateTime.now());
        resourceService.save(resource);
        return new ResponseResult(200, "创建成功");
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除资源")
    public ResponseResult delete(@PathVariable @ApiParam(value = "资源ID", required = true) Long id) {
        resourceService.removeById(id);
        return new ResponseResult(200, "删除成功");
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改资源信息")
    public ResponseResult update(@RequestBody Resource resource,
                                 @PathVariable @ApiParam(value = "资源ID", required = true) Long id) {
        resource.setId(id);
        resourceService.updateById(resource);
        return new ResponseResult(200, "修改成功");
    }

    @GetMapping("/list")
    @ApiOperation("分页查询资源列表")
    public ResponseResult list(@RequestParam(required = false) @ApiParam("资源名称关键字") String nameKeyword,
                               @RequestParam(required = false) @ApiParam("资源路径关键字") String urlKeyword,
                               @RequestParam(required = false) @ApiParam("资源分类ID") Integer categoryId,
                               @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "10") @ApiParam(value = "每页条数", required = true) Integer pageSize) {
        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(nameKeyword))
            queryWrapper.like("name", nameKeyword);
        if (!StringUtils.isEmpty(urlKeyword))
            queryWrapper.like("url", urlKeyword);
        if (categoryId != null)
            queryWrapper.eq("category_id", categoryId);
        Page page = new Page(pageNum, pageSize);
        resourceService.page(page, queryWrapper);
        return new ResponseResult(200, "查询完成", page);
    }

    @GetMapping("/listAll")
    @ApiOperation("查询全部资源")
    public ResponseResult listAll() {
        return new ResponseResult(200, "查询完成", resourceService.list());
    }
}
